package com.grace.cms.admin.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import com.grace.cms.common.event.PublicPublishEvent;
import com.grace.cms.admin.mapper.CmsWebMapper;
import com.grace.cms.admin.service.ICmsWebService;
import com.grace.cms.common.domain.entity.CmsWeb;
import com.grace.cms.common.enums.RecordTypeEnums;
import com.grace.cms.common.enums.EventTypeEnums;
import com.grace.cms.common.model.EventModel;
import com.grace.cms.query.mapper.ThemeSearchMapper;
import com.grace.cms.query.mapper.WebSearchMapper;
import com.grace.common.exception.ServiceException;
import com.grace.common.utils.DateUtils;

/**
 * 站点Service业务层处理
 * 
 * @author zhiGe
 * @date 2022-12-06
 */
@Service
public class CmsWebServiceImpl implements ICmsWebService {
    @Autowired
    private CmsWebMapper cmsWebMapper;

    @Autowired
    private WebSearchMapper webSearchMapper;

    @Autowired
    private  ThemeSearchMapper themeSearchMapper;

    @Autowired
    private ApplicationEventPublisher app;

    /**
     * 查询站点
     * 
     * @param webId 站点主键
     * @return 站点
     */
    @Override
    public CmsWeb selectCmsWebByWebName(String webName) {
        return webSearchMapper.selectCmsWebByWebName(webName);
    }

    /**
     * 查询站点列表
     * 
     * @param cmsWeb 站点
     * @return 站点
     */
    @Override
    public List<CmsWeb> selectCmsWebList(CmsWeb cmsWeb) {
        return webSearchMapper.selectCmsWebList(cmsWeb);
    }

    /**
     * 新增站点
     * 
     * @param cmsWeb 站点
     * @return 结果
     */
    @Override
    public int insertCmsWeb(CmsWeb cmsWeb) {
        cmsWeb.setCreateTime(DateUtils.getNowDate());
       int re=   cmsWebMapper.insertCmsWeb(cmsWeb);
         app.publishEvent(new PublicPublishEvent(this, new EventModel(EventTypeEnums.ADD,
			RecordTypeEnums.THEME, cmsWeb.getWebName())));
        return re;
    }

    /**
     * 修改站点
     * 
     * @param cmsWeb 站点
     * @return 结果
     */
    @Override
    public int updateCmsWeb(CmsWeb cmsWeb) {
        cmsWeb.setUpdateTime(DateUtils.getNowDate());
        int re = cmsWebMapper.updateCmsWeb(cmsWeb);
        app.publishEvent(new PublicPublishEvent(this, new EventModel(EventTypeEnums.UPDATE,
			RecordTypeEnums.THEME, cmsWeb.getWebName())));
        return re;
    }

    /**
     * 批量删除站点
     * 
     * @param webNames 需要删除的站点主键
     * @return 结果
     */
    @Override
    public int deleteCmsWebByWebNames(String[] webNames) {
        for (String webName : webNames) {
            this.deleteCmsWebByWebName(webName);
        }
        return webNames.length;
    }

    /**
     * 删除站点信息
     * 
     * @param webName 站点主键
     * @return 结果
     */
    @Override
    public int deleteCmsWebByWebName(String webName) {
        if (themeSearchMapper.selectWebUnionThemeCount(webName)>0) {
            new ServiceException(webName+"主题下有主题关联，请先解除关联后继续操作");
        }
        int re = cmsWebMapper.deleteCmsWebByWebName(webName);
        app.publishEvent(new PublicPublishEvent(this, new EventModel(EventTypeEnums.DELETE,
			RecordTypeEnums.THEME, webName)));
        return re;
    }
}
